package scenic.show;

import scenic.Image;

/**
 * This class offers an easy to use interface for playing videos. 
 * The video is not displayed directly on the screen. Instead the video
 * is written in to an image that can be used as any other image. Currently
 * this class is only implemented on Windows using DirectShow. On Linux
 * the class does nothing.
 */
public class SimpleMediaFile {
	private int id;
	private Image image = new Image();
	
	/**
	 * Creates a media player with the given media file.
	 * 
	 * @param filename Name of the media file.
	 */
	public SimpleMediaFile(String filename) {
		id = create(filename, image);
	}
	
	private native int create(String filename, Image image);
	private native void free(int id);
	private native void play(int id);
	private native void pause(int id);
	private native double getCurrentPosition(int id);
	private native void setCurrentPosition(int id, double position);
	private native double getLength(int id);
	
	protected void finalize() {
		free();
	}
	
	/**
	 * Frees the resources associated with this object. This method
	 * must be called, otherwise the resources are not freed.
	 */
	public void free() {
		if(id > 0)
		{
			free(id);
			id = 0;
		}
	}
	
	/**
	 * Returns the image that contains the video.
	 * 
	 * @return the image
	 */
	public Image getImage() {
		return image;
	}
	
	/**
	 * Starts playing the video.
	 */
	public void play() {
		play(id);
	}
	
	/**
	 * Pauses the video.
	 */
	public void pause() {
		pause(id);
	}
	
	/**
	 * Returns the current position.
	 * 
	 * @return Current position in seconds.
	 */
	public double getCurrentPosition() {
		return getCurrentPosition(id);
	}
	
	/**
	 * Sets the current position.
	 * 
	 * @param position The new position in seconds.
	 */
	public void setCurrentPosition(double position) {
		setCurrentPosition(id, position);
	}
	
	/**
	 * Returns the length of the video file.
	 * 
	 * @return Length of the video in seconds.
	 */
	double getLength() {
		return getLength(id);
	}
}
